<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Actions\Util;

use PsiFramework\Classes\Util\UI\LanguageManager;

use PsiFramework\Classes\Util\Object\Image;
use PsiFramework\Classes\Util\Object\File;

import("Classes.Actions.AbstractAction");
import("Classes.Util.Object.Identification");

use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Applications\DefaultApp\Classes\Model\Trace;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The Upload Action
 *
 * This action is used to upload files
 *
 * @package Actions.Util
 */
class UploadAction extends AbstractAction
{

    /**
     * Function executed by the controller
     *
     * It crops the image
     *
     * @return Response    the response to the action execution
     */
    public function execute()
    {
        parent::execute();
        
        $uploadDir = 'uploads';
        $jsonArray = null;
        $allowedExt = array('jpg','jpeg','png','gif');
        
        
        if (strtolower($this->_request->getServerParameter('REQUEST_METHOD')) != 'post') {
            $jsonArray = $this->buildJsonArray(false, LanguageManager::getInstance()->getText('WRONG_REQUEST'));
        }
        
        try{
            $pic = $this->_request->getFile('pic');
        
            if (!in_array($pic->getExtension(), $allowedExt)) {
                $jsonArray = $this->buildJsonArray(false, LanguageManager::getInstance()->getText('EXTENSIONS_ERROR').implode(',', $allowedExt));
            } else {        
                
                // Move the uploaded file from the temporary
                // directory to the uploads folder:
                if ($pic->moveTo($uploadDir)) {
                    $jsonArray = $this->buildJsonArray(true, LanguageManager::getInstance()->getText('FILE_UPLOADED_SUCCESSFULLY'), $pic->getPath());
                } else {
                    $jsonArray = $this->buildJsonArray(false, LanguageManager::getInstance()->getText('ERROR_DURING_FILE_MOVING'));
                }
            }
        
        } catch (\Exception $e) {
            if ($e->getCode() == -1) {
                $jsonArray = $this->buildJsonArray(false, LanguageManager::getInstance()->getText('ERROR_RETRIEVING_FILE'));
            } else {
                $message = '';
                switch ($e->getCode()){
                    case 1: // UPLOAD_ERR_INI_SIZE
                        $message = LanguageManager::getInstance()->getText('UPLOAD_ERR_INI_SIZE');
                        break;
                    case 2: // UPLOAD_ERR_FORM_SIZE
                        $message =  LanguageManager::getInstance()->getText('UPLOAD_ERR_FORM_SIZE');
                        break;
                    case 3: // UPLOAD_ERR_PARTIAL
                        $message =  LanguageManager::getInstance()->getText('UPLOAD_ERR_PARTIAL');
                        break;
                    case 4: // UPLOAD_ERR_NO_FILE
                        $message =  LanguageManager::getInstance()->getText('UPLOAD_ERR_NO_FILE');
                        break;
                }
                $jsonArray = $this->buildJsonArray(false, $message);
            }
        }
        
        $this->_response->setAttribute('objects', $jsonArray);
        
        $this->findForward("success");
    }
    
    /**
     * Returns the array for json encoding
     * 
     * @param bool   $status
     * @param string $str
     * @param string $file
     * 
     * @return array
     */
    function buildJsonArray($status, $str, $file=null)
    {
        return array('status'=>$status, 'message'=>$str, 'file'=>$file, 'path'=>realpath($file));
    }
}